package com.itc.bi.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itc.bi.constant.DataDirectorConstant;
import com.itc.bi.dto.bigScreen.BigScreenTypeDTO;
import com.itc.bi.entity.BigScreenPageEntity;
import com.itc.bi.entity.BigScreenTypeEntity;
import com.itc.bi.entity.DirectoryEntity;
import com.itc.bi.mapper.BigScreenPageMapper;
import com.itc.bi.service.BigScreenTypeService;
import com.itc.bi.mapper.BigScreenTypeMapper;
import com.itc.bi.vo.bigScreenVO.DataSetTreeVO;
import com.itc.bi.vo.bigScreenVO.ScreenTypeTreeVO;
import com.itc.bi.vo.bigScreenVO.ScreenTypeVO;
import com.itc.common.core.exception.ServiceException;
import com.itc.common.core.utils.StringUtils;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
* @author hecai
* @description 针对表【big_screen_type(大屏分类)】的数据库操作Service实现
* @createDate 2024-06-18 14:54:10
*/
@Service
public class BigScreenTypeServiceImpl extends ServiceImpl<BigScreenTypeMapper, BigScreenTypeEntity> implements BigScreenTypeService{

    @Autowired
    private BigScreenPageMapper bigScreenPageMapper;

    /**
     * 保存大屏分类
     * @param dto
     * @return
     */
    public Boolean saveBigScreenType(BigScreenTypeDTO dto){
        if(StringUtils.isEmpty(dto.getType())){
            throw new ServiceException("类型不能为空");
        }
        if(ObjectUtil.isEmpty(dto.getPid())){
            dto.setPid(0L);
        }
        BigScreenTypeEntity bigScreenTypeEntity = BeanUtil.copyProperties(dto, BigScreenTypeEntity.class);
        Long cout = this.count(Wrappers.<BigScreenTypeEntity>lambdaQuery()
                .eq(BigScreenTypeEntity::getName, dto.getName())
                .eq(BigScreenTypeEntity::getPid, dto.getPid())
                .eq(BigScreenTypeEntity::getType, dto.getType()));
        if(cout>0){
            throw new ServiceException("目录名称重复，请修改");
        }
        bigScreenTypeEntity.setCode(dto.getType()+"_"+ RandomStringUtils.randomAlphanumeric(10));
        return this.save(bigScreenTypeEntity);
    }

    /**
     * 更新大屏分类
     * @param dto
     * @return
     */
    public Boolean updateBigScreenType(BigScreenTypeDTO dto){
        if(ObjectUtil.isEmpty(dto.getId())){
            throw new ServiceException("id不能为空");
        }
        BigScreenTypeEntity screenTypeEntity = this.getById(dto.getId());
        //判断类型名称是否重复
        long count = this.count(Wrappers.<BigScreenTypeEntity>lambdaQuery()
                .eq(BigScreenTypeEntity::getName, dto.getName())
                .eq(BigScreenTypeEntity::getPid, dto.getPid())
                .ne(BigScreenTypeEntity::getId, dto.getId()));
        if(count>0){
            throw new ServiceException("目录名称重复，请修改");
        }
        screenTypeEntity.setName(dto.getName());
        screenTypeEntity.setOrderNum(dto.getOrderNum());
        return this.updateById(screenTypeEntity);
    }

    /**
     * 删除大屏分类
     * @param id
     * @return
     */
    public Boolean deleteBigScreenType(Long id){
        if(ObjectUtil.isEmpty(id)){
            throw new ServiceException("id不能为空");
        }
        //TODO 此处还差逻辑  存在大屏 或其他组件信息  则不能删除大屏
        BigScreenTypeEntity screenTypeEntity = this.getById(id);
        if(ObjectUtil.isEmpty(screenTypeEntity)){
            throw new ServiceException("类型不存在，或已被删除");
        }

        //存在同级目录不能删除
        if(this.count(Wrappers.<BigScreenTypeEntity>lambdaQuery()
                .eq(BigScreenTypeEntity::getId, screenTypeEntity.getPid()))>1){
            throw new ServiceException("存在同级目录，不能删除");
        }

        Long count = bigScreenPageMapper.selectCount(Wrappers.<BigScreenPageEntity>lambdaQuery().eq(BigScreenPageEntity::getParentCode, screenTypeEntity.getCode()));
        if(count>0){
            throw new ServiceException("存在态势信息，不能删除");
        }
        return this.removeById(id);
    }

    /**
     * 根据类型获取大屏分类列表
     * @param type
     * @return
     */
    public List<ScreenTypeVO> getListByTYpe(String type){
        List<BigScreenTypeEntity> list = this.list(Wrappers.<BigScreenTypeEntity>lambdaQuery()
                .eq(BigScreenTypeEntity::getType, type)
                .orderByAsc(BigScreenTypeEntity::getOrderNum));
        List<ScreenTypeVO> screenTypeVOS = BeanUtil.copyToList(list, ScreenTypeVO.class);
        return screenTypeVOS;
    }

    /**
     * 根据类型获取大屏分类列表
     * @param type
     * @return
     */
    public List<ScreenTypeTreeVO> getListByTypeTree(String type){
        List<BigScreenTypeEntity> list = this.list(Wrappers.<BigScreenTypeEntity>lambdaQuery()
                .eq(BigScreenTypeEntity::getType, type)
                .orderByAsc(BigScreenTypeEntity::getOrderNum)
                .orderByDesc(BigScreenTypeEntity::getCreateTime));
        return getDirectTree(0L, list);
    }

    /**
     * 构建树结构
     * @param pid
     * @param screenTypeVOS
     * @return
     */
    public List<ScreenTypeTreeVO> getDirectTree(Long pid, List<BigScreenTypeEntity> list){
        List<ScreenTypeTreeVO> children = new ArrayList<>();
        for (BigScreenTypeEntity entity : list){
            ScreenTypeTreeVO vo = new ScreenTypeTreeVO();
            if (Objects.equals(entity.getPid(), pid)){
                vo.setChrildren(getDirectTree(entity.getId(),list));
                vo.setId(entity.getId());
                vo.setName(entity.getName());
                vo.setPid(entity.getPid());
                vo.setCode(entity.getCode());
                vo.setOrderNum(entity.getOrderNum());
                vo.setType(entity.getType());
                children.add(vo);
            }
        }
        return children;
    }

    /**
     * 根据编码获取分类信息
     * @param code
     * @return
     */
    @Override
    public BigScreenTypeEntity getInfoByCode(String code){
        return this.getOne(Wrappers.<BigScreenTypeEntity>lambdaQuery()
                .eq(BigScreenTypeEntity::getCode, code));
    }
}




